#!/usr/bin/perl
# password generator client (PROTOTYPE)
# Peter Walsh csci 265

$| = 1;

use warnings;
use strict;


use IO::Socket;


my @command;
@command = @ARGV;

my $numofARG=$#command;

my $length=8;
my $type="num";




for(my $i=0;$i<=$numofARG;$i++){
   if($command[$i] eq "-v"){

      print "Version: 0.1\n";


   }elsif($command[$i]eq"-t") {

      if($i<$numofARG){
         if($command[$i+1]eq"num"){

            $type=$command[$i+1];
            $i++;


      
         }elsif($command[$i+1]eq"alnum"){

            $type=$command[$i+1];
            $i++;



         }else{

         die "Error: Invalid Type\n";

         }
      }else{
         die "Error: Invalid Type\n";
      }
   }elsif($command[$i]eq"-n"){

      if($i<$numofARG){

         if(($command[$i+1])=~(m/[0-9]/)){
            if(($command[$i+1])=~(m/[[a-z]/i)){
               die "Error: Invalid Lenght\n";
            }else{
               $length=$command[$i+1];
               if(defined($length)){
                  if(($length<2)||($length>64)){
                     die "Error: Invalid Length\n";
                  }else{
                   $i++;
                  }
               }else{
                  die "Error: Invalid Length\n";
               }
            }

         }else{

            die "Error: Invalid Length\n";


         }
      }else{
         die "Error: Invalid Length:\n";
      }
   }else{

   die "Error: Invalid Switch\n";


   }

}# ends arugment token loop

# values to use to connect to real server
# PeerAddr = 192.168.18.21
# PeerPort = 7071


my $sock;

$sock = new IO::Socket::INET (
                              PeerAddr => 'LocalHost',
                              PeerPort => '7072',
                              Proto => 'tcp'
                             );
die "Error: Unable to Connect To Password Server\n" unless $sock;



print $sock "1:$type:$length\n";


while (1) {
my $pass;
   $pass = <$sock>;
   chop $pass;
   last if $pass eq "CLOSE";
   print "Password: $pass \n";
}

